package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.common.service.UploadService;
import com.luchao.back.pojo.Ads;
import com.luchao.back.service.AdsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "广告管理")
@RestController
@RequestMapping("/ads")
public class AdsController {

    @Value("${file.upload.path}")
    private String path;

    @Autowired
    private AdsService adsService;

    @Autowired
    private UploadService uploadService;

    @ApiOperation("分页获取广告信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAdsByPage")
    public ResponseResult getAdsByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = adsService.getAdsCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Ads> pageResult = new PageResult<Ads>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(adsService.getAdsByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("上传广告图片")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "flimage", value = "上传广告图片文件对象", required = true, paramType = "query", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/uploadImage")
    public ResponseResult uploadImage(@RequestPart MultipartFile flimage) {
        return uploadService.uploadPhoto(path, flimage) ? ResponseResult.success(ResponseResult.SUCCESS, "上传成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "上传失败", Execute.ERROR);
    }

    @ApiOperation("校验广告名称唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "adcompany", value = "广告名称", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkAds")
    public ResponseResult checkAds(String adcompany) {
        return adsService.checkAds(adcompany) > 0 ? ResponseResult.success(ResponseResult.FAILED, "公司名称已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "公司名称不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("添加新的广告")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ads", value = "广告对象", required = true, paramType = "formData", dataType = "Ads", dataTypeClass = Ads.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addAds")
    public ResponseResult addAds(@RequestBody Ads ads) {
        return adsService.addAds(ads) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("删除广告")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "adid", value = "广告ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/deleteAds")
    public ResponseResult deleteAds(Integer adid) {
        return adsService.deleteAds(adid) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "删除成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "删除失败", Execute.ERROR);
    }

    @ApiOperation("批量删除广告")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ads", value = "广告列表", required = true, paramType = "formData", dataType = "List<Ads>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchDeleteAds")
    public ResponseResult batchDeleteAds(@RequestBody List<Ads> ads) {
        return adsService.batchDeleteAds(ads) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "操作失败", Execute.ERROR);
    }
}
